package com.huahua.bigdata.spark.rdd.operate;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaSparkContext;
import scala.Tuple2;

import java.util.Arrays;

public class Spark12_Operate_Transform_KV_wordCount {
    public static void main(String[] args) {
        SparkConf conf = new SparkConf();
        conf.setMaster("local");
        conf.setAppName("spark");
        JavaSparkContext jsc = new JavaSparkContext(conf);
        JavaPairRDD<String, Integer> pairRDD = jsc.parallelizePairs(
                Arrays.asList(
                        new Tuple2<>("a", 1),
                        new Tuple2<>("b", 2),
                        new Tuple2<>("a", 3),
                        new Tuple2<>("b", 4)
                )
        );
        // TODO 将分组聚合功能进行简化操作
        //      redceByKey方法的作用: 将KV类型的数据按照 K 对 V 进行reduce(将多个值聚合成一个值)操作
        //          计算的基本思想： 两个计算
        JavaPairRDD<String, Integer> wordCountRDD = pairRDD.reduceByKey(Integer::sum);
        wordCountRDD.collect().forEach(System.out::println);

        jsc.close();
    }
}
